<template>
  <div class=''>

    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card class="box-card">

      <!--  -->
      <el-row :gutter="20">
        <el-col :span='8'>
          <el-input
          v-model="queryInfo.query" placeholder="请输入内容"
          clearable @clear="getGoodsList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span='4'>
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price"  width="110px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="90px"></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="160px">
          <template slot-scope="scope">
          {{scope.row.add_time | dataFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
              <!-- 修改按钮 -->
              <el-button
              type="primary" icon="el-icon-edit" size="mini"
              ></el-button>
              <!-- 删除按钮 -->
              <el-button
              type="danger" icon="el-icon-delete" size="mini"
              @click="removeGoodById(scope.row.goods_id)"
              ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[1, 10, 20, 50]"
        :page-size="queryInfo.pagesize"
        @size-change="handleSizeChange"
        :current-page="queryInfo.pagenum"
        @current-change="handleCurrentChange"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>

export default {
  name: 'Goods',
  props: {

  },
  data () {
    return {
      goodsList: [],
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表数据失败')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },

    // 监听 pagesize 改变事件
    handleSizeChange (NewSize) {
      this.queryInfo.pagesize = NewSize
      this.getGoodsList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange (NewPage) {
      this.queryInfo.pagenum = NewPage
      this.getGoodsList()
    },
    // 删除商品
    async removeGoodById (id) {
      // 弹框提示用户是否删除
      const confirmResult = await this.$confirm('此操作将永久删除该商品', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning'
      }).catch(err => {
        return err
      })

      // 判断是否发起删除商品请求
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }

      // 发起删除商品请求
      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除商品失败')
      }
      this.$message.success('删除商品成功')

      // 重新获取用户列表
      this.getGoodsList()
    },
    // 跳转到添加商品页面
    goAddpage () {
      this.$router.push('/goods/add')
    }
  }
}
</script>
<style  scoped>
.el-breadcrumb{
  margin-bottom: 15px;
  font-size: 12px;
}
.box-card{
  box-shadow:  0 1px 1px rgba(0, 0, 0, 0.15) !important;
}
.title{
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.el-table{
  font-size: 12px;
  margin: 15px 0;
}
</style>
